import { cartList , delcart } from '@/api/shopcart.js'
export default {
	state: {
		list: [
			// {
			// 	checked: false,
			// 	id: 1,
			// 	name: '打客服的',

			// 	imgUrl: 'https://cdn.uviewui.com/uview/swiper/swiper1.png',
			// 	pprice: '27.3',
			// 	num: 1
			// },
			// {
			// 	checked: false,
			// 	id: 2,
			// 	name: '打客服的和非恶搞认为微软业务打客服',

			// 	imgUrl: 'https://cdn.uviewui.com/uview/swiper/swiper1.png',
			// 	pprice: '27.3',
			// 	num: 1
			// },
			// {
			// 	checked: false,
			// 	id: 3,
			// 	name: '打客服的和非恶搞认为微软业务打客服的和非恶搞认为微软业务',

			// 	imgUrl: 'https://cdn.uviewui.com/uview/swiper/swiper1.png',
			// 	pprice: '27.3',
			// 	num: 1
			// }
		],
		selectedList: []
	},
	getters: {
		//判断是否  全选
		checkedAll(state) {
			return state.list.length === state.selectedList.length;
		},
		//合计+结算数量
		totalCount(state) {
			let total = {
				pprice: 0,
				num: 0
			}
			state.list.forEach(v => {
				//是否选中
				if (state.selectedList.indexOf(v.id) > -1) {
					//合计
					total.pprice += v.product_sku.price * v.amount;
					//结算数量
					total.num = state.selectedList.length;
				}
			})
			total.pprice = parseFloat(total.pprice).toFixed(2)
			return total;
		}
	}, 
	mutations: {
		//全选
		checkAll(state) {
			state.selectedList = state.list.map(v => {
				v.checked = true;
				return v.id;
			})
		},
		//全不选
		unCheckAll(state) {

			state.list.forEach(v => {
				v.checked = false;
			})
			state.selectedList = [];
		},
		 updateCartList(state, newList) {
            state.list = newList; // 更新购物车列表数据
			state.list.forEach(item => {
				item.checked = true
			})
			state.selectedList = state.list.map(v => {
				v.checked = true;
				return v.id;
			})
        },
		
		//单选
		selectedItem(state, index) {
			let id = state.list[index].id;
			let i = state.selectedList.indexOf(id);
			//如果selectedList已经存在就代表他之前是选中状态, checked=false,并且在selectedList删除
			if (i > -1) {
				state.list[index].checked = false;
				return state.selectedList.splice(i, 1);
			}
			//如果之前没有选中,checked=true , 把当前的id添加到selectedList
			state.list[index].checked = true;
			state.selectedList.push(id);
		},
		delGoods(state) {
			const deldata = state.list.filter(v => {
				return state.selectedList.indexOf(v.id) != -1;
			})
			deldata.forEach(item => {
				delcart(item.product_sku_id).then(res => {
					
				})
			})
			// state.list = state.list.filter(v => {
			// 	return state.selectedList.indexOf(v.id) === -1;
			// })
		},
		//加入购物车
		addShopCart(state, goods) {
			state.list.push(goods);
		},
		removecart(state){
			state.list = []
		},
		
	},
	actions: {
		checkedAllFn({
			commit,
			getters
		}) {
			getters.checkedAll ? commit("unCheckAll") : commit("checkAll");
		},
		delGoodsFn({
			commit , dispatch
		}) {

			commit('delGoods');
			commit('unCheckAll');
			setTimeout(() => {
				commit('removecart');
				dispatch('fetchCartList');
			},1000)
			
																			
			uni.showToast({
				title: '删除成功',
				icon: "none"
			})	


		},
		// 购物车列表
		fetchCartList({ commit }) {
		    cartList().then(res => {
		        if (res.status == 200) {
		            commit('updateCartList', res.data); // 触发 mutation 更新 cartList
		        }
		    }).catch(error => {
		        console.error('Error fetching cart list:', error);
		    });
		}
	}
}